﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTcpSocket>
#include "RngLib.h"
#include "qcustomplot.h"

#define TXHead 0xABFBCDFB
#define TXTial 0x0FAFBFCF
#define agility_id 0x02
#define freqlist_id 0x03
#define usersetagility_id 0x22
#pragma pack(push, 1)  // 设置1字节对齐
// UAV测向采数数据结构
typedef struct{
    uint32_t head;
    uint8_t id;
    uint32_t sum;
    uint16_t len;
    uint8_t anneten;//0x01定向、0x02全向、0x03未知
    uint16_t freq;
    uint64_t timestamp;
    uint8_t uavrssi[1024];
    uint32_t tial;
}UAVRSSIDATA;

typedef struct{
    uint32_t head;
    uint8_t id;
    uint32_t sum;
    uint16_t len;
    uint16_t freq;
    uint8_t span;   
    uint16_t time_s; 
    uint32_t tail;
}USER_SETAGILITYFREQ;

typedef struct{
    uint32_t head;
    uint8_t id;
    uint32_t sum;
    uint16_t len; 
}HEAD_DATA_MESSAGE;


typedef struct{
    uint32_t head;
    uint8_t id;
    uint32_t sum;
    uint16_t len;
    uint8_t num;
    uint16_t data[128];
}RXFREQ_DATA_MESSAGE;




typedef struct{
	uint32_t head;
	uint8_t id;
	uint32_t sum;
	uint16_t len;
	uint8_t data;
	uint32_t tail;
}TX_DATA_MESSAGE;

typedef enum{
    agility_on = 0x01,
    agility_off = 0x02,
    agility_auto = 0x03
}agility_mode;


typedef struct{
    uint32_t head;
    uint8_t id;
    uint32_t sum;
    uint16_t len;
    uint16_t freq[10];  //打击频点
    uint8_t span[10];   //带宽
    uint16_t time_s[10]; //时间s
    uint8_t size;   //设置频点个数
    uint32_t tail;
}USER_SETAGILITYFREQ_gai;


#pragma pack(pop)  // 恢复默认对齐


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_openButton_clicked();
    void on_clearButton_clicked();
    void on_sendButton_clicked();
    void on_tcpButton_clicked();
    void on_cx_pushButton_clicked();
    void on_exitcx_pushButton_clicked();
    void on_IAPpushButton_clicked();
    void on_APPpushButton_clicked();
    void on_expandfreq_Button_clicked();



    void on_in_mjmode_pushButton_clicked();

    void on_exit_mjmode_pushButton_clicked();

    void on_Jam_pushButton_clicked();

    void on_exit_JampushButton_clicked();

    void on_Jam_pushButton_2_clicked();

    void on_exit_JampushButton_2_clicked();

private:
    Ui::MainWindow *ui;
    QSerialPort *serialPort;
    QTcpSocket *tcpSocket;  // 添加TCP Socket
    void initSerialPort();
    void updatePortList();
    void readData();
    void initTcpSocket();  
    void parseMessage(const QString &message);
    void findHeartbeat(const QString &data);
    void setupPlot();
    void parseMessageDirectly(const QString &message);
    void processRingBufferData();
    

    
    // 数据缓冲区
    QByteArray dataBuffer;
    bool isInDataCollectionMode;
    RING_ID serialRingBuffer;
    bool isExpanded = false;
};
#endif // MAINWINDOW_H
